const fs = require('fs')
const readline = require('readline')
const os = require('os')

// 配置增删改查模块

// 配置数据结构

// 读取配置

// 写入配置
function saveRuleConfig (stream, outputPath = './rule-config.json') {
  return new Promise((resolve, reject) => {
    const ruleConfigStream = fs.createWriteStream(outputPath, {
      flags: 'w',
      encoding: 'utf8'
    })
    ruleConfigStream.on('ready', (error, res) => {
      console.log('ready', error, res)
    })
    ruleConfigStream.on('drain', (error, res) => {
      console.log('drain', error, res)
    })
    ruleConfigStream.on('error', (error, res) => {
      console.log('error', error, res)
      reject({
        code: 1,
        message: 'error'
      })
    })
    ruleConfigStream.on('finish', (error, res) => {
      console.log('finish', error, res)
    })
    ruleConfigStream.on('pipe', (error, res) => {
      console.log('pipe', error, res)
    })
    ruleConfigStream.on('unpipe', (error, res) => {
      console.log('unpipe', error, res)
    })
    ruleConfigStream.on('close', (error, res) => {
      console.log('close', error, res)
    })

    ruleConfigStream.write(stream, 'utf8', () => {
      console.log('write end')
      resolve({
        code: 0,
        message: 'succeed'
      })
    })
  })
}

module.exports = {
  saveRuleConfig
}